package com.jinzhi.jzweb.strategy.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ExaminationAnswerSheetComDO;
import com.jinzhi.jzweb.domain.ExaminationResultsComDO;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.pojo.AsourVo;
import com.jinzhi.jzweb.pojo.StuSignUpComNum2PoVo;
import com.jinzhi.jzweb.pojo.VtatisticsVo;
import com.jinzhi.jzweb.service.ExaminationAnswerSheetComService;
import com.jinzhi.jzweb.service.ExaminationBatchComSService;
import com.jinzhi.jzweb.service.ExaminationResultsComService;
import com.jinzhi.jzweb.service.StuSignUpComService;
import com.jinzhi.jzweb.strategy.BaseStrategy;
import com.jinzhi.jzweb.strategy.factory.SourConfig;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/*
 *  @author : JingChangTian
 *  @date   : 2023/8/11 20:20
 *  @description :  A赛场
 *
 */
@Component(value = "A")
public class ExaminationASour extends BaseController implements BaseStrategy  {

    private final StuSignUpComService stuSignUpService;

    private final ExaminationResultsComService examinationResultsComService;

    private final ExaminationAnswerSheetComService examinationAnswerSheetComService;

    private final ExaminationBatchComSService examinationBatchComSService;


    public ExaminationASour(StuSignUpComService stuSignUpService,
                            ExaminationResultsComService examinationResultsComService,
                            ExaminationAnswerSheetComService examinationAnswerSheetComService, ExaminationBatchComSService examinationBatchComSService) {
        this.stuSignUpService = stuSignUpService;
        this.examinationResultsComService = examinationResultsComService;
        this.examinationAnswerSheetComService = examinationAnswerSheetComService;
        this.examinationBatchComSService = examinationBatchComSService;
    }

    /**
     * A赛场分数统计业务层
     * @param examinationAnswerSheetComDTO
     * @return
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    @Override
    public Result<VtatisticsVo> list(ExaminationResultsComDO examinationAnswerSheetComDTO) throws NoSuchFieldException, IllegalAccessException {
        VtatisticsVo vtatisticsVo = new VtatisticsVo();
        return Result.ok(vtatisticsVo);
    }

    @Override
    public Page<StuSignUpComDO> getStuSignByCourt2(String id,Integer curr,Integer pageSize) {
        Page<StuSignUpComDO> page = new Page<>(
                Optional.of(curr).orElse(1),
                Optional.of(pageSize).orElse(10));
        //根据赛场id查询考生信息，理论赛场查询全部
        Page<StuSignUpComDO> sign = stuSignUpService.selectPage(page,new EntityWrapper<StuSignUpComDO>().
                eq(StringUtils.isNotBlank(id),"court2",id).
                isNotNull("court2"));
        //根据考生id获取ppt
        sign.getRecords().forEach(a ->{
            //二次加密验证后
            if(StringUtils.isNotBlank(a.getNum2())){
                a.setPptfile(Optional.ofNullable(examinationAnswerSheetComService.
                        findOneByKv("stu_sign_id",a.getId())).orElse(new ExaminationAnswerSheetComDO()).getPptfile());
            }
        });
        sign.setRecords(sign.getRecords().stream().sorted(Comparator.comparing(StuSignUpComDO::getCourt2,Comparator.nullsLast(String::compareTo)).
                thenComparing(StuSignUpComDO::getRealityNm2,Comparator.nullsLast(String::compareTo))).
                collect(Collectors.toList()));

        //根据学生id查询成绩
        ExaminationResultsComDO examinationResultsComDO = new ExaminationResultsComDO();
        sign.getRecords().forEach(a ->{
            examinationResultsComDO.setStuSignId(a.getId());
            a.setExaminationResultsComDO(examinationResultsComService.selectOne(examinationResultsComDO));
        });
        return sign;
    }

    @Override
    public String pageReturn() {
        return "jzweb/transcribe/recordpointsA";
    }

    @Override
    public String markReturn(Integer type) {
        if(type == 1){
            return "jzweb/transcribe/gradeA";
        }else if(type == 2){
            return "jzweb/transcribe/gradeAPo";
        }
        return null;
    }

    @Override
    public void downloadImportTemplate(HttpServletResponse response,String id) {
        List<AsourVo> asourVos = new ArrayList<>();
        //根据赛场id查询考生信息，理论赛场查询全部
        List<StuSignUpComDO> sign = stuSignUpService.selectList(new EntityWrapper<StuSignUpComDO>().
                eq(StringUtils.isNotBlank(id),"court2",id).
                isNotNull("court2"));
        sign.stream().sorted(Comparator.comparing(StuSignUpComDO::getCourt2,Comparator.nullsLast(String::compareTo)).
                thenComparing(StuSignUpComDO::getRealityNm2,Comparator.nullsLast(String::compareTo))).
                collect(Collectors.toList());

        //根据学生id查询成绩
        ExaminationResultsComDO examinationResultsComDO = new ExaminationResultsComDO();
        sign.forEach(a ->{
            AsourVo asourVo = new AsourVo();
            examinationResultsComDO.setStuSignId(a.getId());
            ExaminationResultsComDO rcd = examinationResultsComService.selectOne(examinationResultsComDO);
            if(rcd!=null) {
                asourVo.setRemarkA(rcd.getRemarkB());
                asourVo.setScoinga1(rcd.getScore1());
                asourVo.setScoinga2(rcd.getScore2());
                asourVo.setScoinga3(rcd.getScore3());

                BigDecimal pjf = new BigDecimal((rcd.getScore1() + rcd.getScore2() + rcd.getScore3()) /3);
                Double pjff = pjf.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                asourVo.setPjf(pjff);
            }
            asourVo.setName(examinationBatchComSService.selectById(a.getCourt2()).getName());
            asourVo.setRealityNm2(a.getRealityNm2());
            asourVos.add(asourVo);
        });
        //赛场名称
        String name = Optional.ofNullable(asourVos.get(0).getName()).orElse("");
        //导出操作
        EasyPoiUtil.exportExcel(asourVos, name+"赛场导出", name+"赛场导出", AsourVo.class, name+"赛场导出", response);
    }


}
